home *** CD-ROM | disk | FTP | other *** search
/ Acorn RISC PD-CD 1 / Acorn RISC PD-CD 1.iso / games / _kalaha / source / c / moves < prev    next >
Encoding:
Text File  |  1992-03-04  |  6.7 KB  |  354 lines

  1.  
  2. /* this deals with the triggering of moves by user
  3. or (by alarm) for computer; the queueing of data for 
  4. later display; and the updating of the window and sound
  5. (also by alarm) */
  6.  
  7. #include "kalaha.h"
  8.  
  9.  
  10.  
  11. static void translate_real_to_working(char * h);
  12. static void update_display(int p, int gain);
  13. static int display_move(char * h, int p);
  14. static void queue_a_plop(int p, int gain);
  15. static void comp_moves(void);
  16. static void real_move(int p);
  17.  
  18. /* control passes to functions in this file via the 
  19.   following two extern functins */
  20.  
  21.  
  22. void icon_hit(wimp_i i)
  23. {
  24. int layer;
  25.  
  26. /* do nothing if move in progress */
  27. if ( plopsdone < nofplops )
  28.   return;
  29.  
  30. /* click on arrow */
  31. if (i==6 && !automove && starterplayer != UservUser)
  32.   {
  33.   comp_moves();
  34.   return;
  35.   }
  36.  
  37. layer = which_layer(i);
  38. if (layer != TOPLAY  && layer != BOTLAY)
  39.   return;
  40. /* Now left with clicks on move icons.  Eliminate illegal moves */
  41. if (layer == TOPLAY  &&  (botgo  ||  top[i-7]==0  ||  starterplayer != UservUser)) 
  42.   return;
  43. if (layer == BOTLAY  &&  ((!botgo) || bot[i]==0)) 
  44.   return;
  45.  
  46. real_move(i);
  47. }
  48.  
  49.  
  50.  
  51.  
  52. void alarm_handler_proc(int calledat, void *handle)
  53. {
  54. handle = handle;
  55.  
  56. if  (plopsdone == nofplops)  /* if no move in progress */
  57.   {
  58.   plopsdone = nofplops = 0;
  59.   if (automove  && starterplayer != UservUser)
  60.     comp_moves();  
  61.   }
  62.  
  63. if (plopsdone < nofplops)
  64.   {
  65.   update_display(pendingplops[plopsdone].p, pendingplops[plopsdone].gain);
  66.   plopsdone += 1;
  67.   }
  68.  
  69. while (calledat <= alarm_timenow())
  70.   calledat += 50;
  71. alarm_set(calledat, alarm_handler_proc, (void *)0 );
  72.  
  73.  
  74.  
  75. void queue_a_plop(int p, int gain)
  76. {
  77. if (p<14 && gain == 0) 
  78.      return;
  79. pendingplops[nofplops].p = p;
  80. pendingplops[nofplops].gain = gain;
  81. nofplops += 1;
  82. }
  83.  
  84.  
  85.  
  86. void comp_moves(void)
  87. {
  88. char here[14];
  89. int move, depth;
  90.  
  91. depth = 5*level/3;
  92. wassert(depth < MAXDEPTH);
  93. if (!botgo  && (top[0]|top[1]|top[2]|top[3]|top[4]|top[5]) && starterplayer != UservUser)
  94.   {
  95.   visdelay_begin();       
  96.   translate_real_to_working(here);
  97.   if (level == 0)
  98.     move = stupid_move(here);
  99.   else
  100.     move = best_n_move(depth, here).move;
  101.   visdelay_end();
  102.   
  103.   real_move( 7 + move );
  104.   }
  105. }
  106.  
  107.  
  108.  
  109.  
  110. void update_display(int p, int gain)
  111.  
  112. /*  p is 0...14,  0...13 being  c-clockwise from mover's botleft
  113. gain is change to h[p], can be 0 or -ve
  114. p>=14 is a special case, meaning update botgo and arrow according to 
  115.  gain which is TRUE if change player, and deselect starting icon (p-14)
  116. always.
  117. */
  118. {
  119. wimp_redrawstr r;
  120. BOOL           more;
  121. int            balls;
  122.  
  123.  
  124. /* convert from mover's to bot's point of view */
  125. if (which_layer(p) != OTHERLAY && !botgo)
  126.   {        
  127.   p += 7;
  128.   if (p>13)
  129.       p -= 14;
  130.   }
  131.  
  132. switch (which_layer(p))
  133.   {
  134.   case BOTLAY:
  135.   bot[p] += gain;
  136.   break;
  137.   case TOPLAY:
  138.   top[p-7] += gain;
  139.   break;
  140.   case MIDRIGHT:
  141.   bottot += gain;
  142.   break;
  143.   case MIDLEFT:  
  144.   toptot += gain;
  145.   break;
  146.   case OTHERLAY:
  147.   if (gain==TRUE)
  148.     botgo = !botgo;
  149.   break;
  150.   }
  151.  
  152. if (which_layer(p) != OTHERLAY  &&  gain>=1  &&  soundon)
  153.   {
  154.   int oldbeats, oldtempo;      
  155.   /* attempt not to interfere with other programs' sound
  156.      - probably doomed to failure anyway */
  157.   oldbeats = bbc_getbeats();   
  158.   oldtempo = bbc_gettempo();
  159.  
  160.   wimpt_noerr(bbc_soundon());
  161.   wimpt_noerr(bbc_settempo(4096));
  162.   wimpt_noerr(bbc_setbeats(0));       
  163.   balls = 1;
  164.   while (balls<gain)
  165.     {
  166.     wimpt_noerr(bbc_sound(1, -13, 103, 1, 5*balls));
  167.     balls += 1;
  168.     }
  169.   wimpt_noerr(bbc_sound(1, -13, 103, 2, 5*gain));
  170.   wimpt_noerr(bbc_sound(1, -13, 108, 1, 5*gain+7));
  171.  
  172.   wimpt_noerr(bbc_settempo(oldtempo));
  173.   wimpt_noerr(bbc_setbeats(oldbeats));  
  174.   }
  175.  
  176. r.w = main_window_handle;
  177. find_hollow_box(&(r.box), p);
  178.   
  179. wimpt_noerr(wimp_update_wind( &r, &more ));
  180. while (more)
  181.   {
  182.   plot_one_hollow(p, r);  
  183.   wimpt_noerr(wimp_get_rectangle(&r, &more));
  184.   }
  185.  
  186.  
  187. switch (which_layer(p))
  188.   {
  189.   case BOTLAY:
  190.   write_iconnum(bot[p], p);
  191.   wimpt_noerr(wimp_set_icon_state(main_window_handle, p, 0, 0));
  192.   break;
  193.   
  194.   case TOPLAY:
  195.   write_iconnum(top[p-7], p);
  196.   wimpt_noerr(wimp_set_icon_state(main_window_handle, p, 0, 0));
  197.   break;
  198.  
  199.   case MIDRIGHT:
  200.   write_iconnum(bottot, 13);
  201.   wimpt_noerr(wimp_set_icon_state(main_window_handle, 13, 0, 0));
  202.   break;
  203.  
  204.   case MIDLEFT:
  205.   write_iconnum(toptot, 14);
  206.   wimpt_noerr(wimp_set_icon_state(main_window_handle, 14, 0, 0));
  207.   break;
  208.   
  209.   case OTHERLAY:
  210.   set_icon_select_state(main_window_handle, p-14, FALSE);
  211.   break;
  212.   }
  213. }
  214.  
  215.  
  216.  
  217.  
  218.  
  219. int display_move(char * h, int p)
  220. /* do a displayed move */
  221. /* 
  222. passed game situation in h[0...13].  posn p, 0<=p<5, is
  223. to move.
  224.              12 11 10  9  8  7
  225.           13                   6
  226.               0  1  2  3  4  5
  227.  
  228. If botgo, this maps directly to screen positions, else add 7 mod 14.
  229. */
  230. /*
  231. Returns: MY,YOUR,OVER according to another go, opp's go, game end
  232.          Updates h[] to new values
  233. */
  234. /* 
  235. this is very similar to virtual_move(), but calls queue_a_plop() to
  236. update the 'real' (global) variables bot[], top[], botot, and 
  237. toptot (but not botgo) and then force a redraw.
  238. */
  239. {
  240. int carry, i, temp;
  241.  
  242. carry = h[p];
  243. h[p] = 0; queue_a_plop(p, -carry);
  244.  
  245. while (carry-->0)
  246.   {
  247.   p++;
  248.   if (p==13) p=0;
  249.   h[p] += 1; queue_a_plop(p, 1);
  250.   }
  251. if (p < 6  &&  h[p] == 1)
  252.   {
  253.   h[p] = 0;  queue_a_plop(p, -1);
  254.   h[6] += 1; queue_a_plop(6,  1);
  255.   temp = h[12-p];
  256.   h[12-p] = 0;  queue_a_plop(12-p, -temp);
  257.   h[6] += temp;  queue_a_plop(6, temp);
  258.   }
  259. if (h[0]==0 && h[1]==0 && h[2]==0 && h[3]==0 && h[4]==0 && h[5]==0)
  260.   {
  261.   for (i=7; i<13; i++)
  262.       {
  263.       temp = h[i];
  264.       h[i] = 0; queue_a_plop(i, -temp);
  265.       h[6] += temp; queue_a_plop(6, temp);
  266.       }
  267.   return OVER;
  268.   }
  269. else if (h[7]==0 && h[8]==0 && h[9]==0 && h[10]==0 && h[11]==0 && h[12]==0)
  270.   {
  271.   for (i=0; i<6; i++)
  272.       {
  273.       temp = h[i];
  274.       h[i] = 0; queue_a_plop(i, -temp);
  275.       h[6] += temp; queue_a_plop(6, temp);
  276.       }
  277.   return OVER;
  278.   }
  279. else
  280.   {
  281.   if (p==6)
  282.       return MY;
  283.   else
  284.       return YOUR;
  285.   }
  286. }
  287.  
  288.  
  289.  
  290. void translate_real_to_working(char * h)
  291. {  
  292. int i;   
  293. if (botgo)
  294.   {
  295.   for (i=0; i<6; i++)
  296.       h[i] = bot[i];
  297.   h[6] = bottot;
  298.   for (i=7; i<13; i++)
  299.       h[i] = top[i-7];
  300.   h[13] = toptot;
  301.   }
  302. else
  303.   {
  304.   for (i=0; i<6; i++)
  305.       h[i] = top[i];
  306.   h[6] = toptot;
  307.   for (i=7; i<13; i++)
  308.       h[i] = bot[i-7];
  309.   h[13] = bottot;
  310.   }
  311. }
  312.    
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319. void real_move(int p)
  320. {
  321. /* now p can be 0...5 or 7...12 depending on whse go. */
  322. char h[14];  
  323.  
  324. if ( (p<0 || p>6)  &&  botgo)
  325.   werr(TRUE, "SNAFU at line %d", __LINE__);
  326. if ( (p<7 || p>12) &&  !botgo)
  327.   werr(TRUE, "SNAFU at line %d", __LINE__);
  328. if (p<6)
  329.   set_icon_select_state(main_window_handle, p, TRUE);
  330. else
  331.   set_icon_select_state(main_window_handle, p, TRUE);
  332.   
  333. translate_real_to_working(h);
  334. /* Now translated into display_move() format */
  335.  
  336. switch ( display_move(h, (p>6) ? p-7 : p) )
  337.   {
  338.   case OVER:       
  339.   break;
  340.  
  341.   case MY:
  342.   queue_a_plop(14+p, FALSE);   
  343.   break;        
  344.  
  345.   case YOUR:  
  346.   queue_a_plop(14+p, TRUE);
  347.   break;
  348.   }
  349. }
  350.  
  351.  
  352.  
  353.